package com.example.springbatch_itemreader_json;

import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecutionListener;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.file.FlatFileItemReader;
import org.springframework.batch.item.file.builder.FlatFileItemReaderBuilder;
import org.springframework.batch.item.file.mapping.FieldSetMapper;
import org.springframework.batch.item.json.JacksonJsonObjectReader;
import org.springframework.batch.item.json.JsonItemReader;
import org.springframework.batch.item.json.JsonObjectReader;
import org.springframework.batch.item.json.builder.JsonItemReaderBuilder;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.core.io.ClassPathResource;

import javax.annotation.Resource;
import java.util.List;

@SpringBootApplication
@EnableBatchProcessing
@Slf4j
public class SpringApplication15 {

    public static void main(String[] args) {
        SpringApplication.run(SpringApplication15.class, args);
    }

    @Resource
    private JobBuilderFactory jobBuilderFactory;

    @Resource
    private StepBuilderFactory stepBuilderFactory;


    @Resource(name = "myJobExecutionListener")
    private JobExecutionListener jobExecutionListener;

    @Bean
    public ItemWriter<User> itemWriter() {
        return new ItemWriter<User>() {
            @Override
            public void write(List<? extends User> list) throws Exception {
                list.forEach(System.out::println);
            }
        };
    }

    /**
     * 以json的格式进行读取
     * @return
     */
    @Bean
    public JsonItemReader<User> jsonItemReader() {
        ObjectMapper objectMapper = new ObjectMapper();
        JacksonJsonObjectReader<User> jsonReader = new JacksonJsonObjectReader<>(User.class);
        jsonReader.setMapper(objectMapper);

        return new JsonItemReaderBuilder<User>().name("userJsonReader")
                .jsonObjectReader(jsonReader)
                .resource(new ClassPathResource("user.json"))
                .build();
    }


    @Bean
    public Step step() {
        return stepBuilderFactory
                .get("step")
                .<User, User>chunk(1)
//                .reader(userItemReader())
                .reader(jsonItemReader())
                .writer(itemWriter())
                .build();
    }

    @Bean
    public Job job() {
        return jobBuilderFactory.get("job-faster-json").start(step()).
                incrementer(new RunIdIncrementer())
                .listener(jobExecutionListener)
                .build();
    }


}
